﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using digitwhu.Models.IRepositorys;
using digitwhu.Models;
using digitwhu.Models.Repositorys;
using digitwhu.Helper;
using digitwhu.Logic.ClassLibrary;
using digitwhu.Logic.BusinessLogic;

namespace digitwhu.Logic.BusinessLogicControl
{
    public class BlogLogicControl
    {
       #region 微博
        public static string GetBlog(string placeId, string type)//获取指定地点所有微博列表，尚未判断是否是自己好友与黑名单
        {
            //type指明查询看看微博的地点。取值只可能是：building,street
            //placeId指明地点编号，可能是building上面的编号，也可能是street上面的编号
            //返回某个地点的所有原创微博编号列表
            int placeid=Int32.Parse (placeId );
            var dbContext = new DigitwhuDataContext();
            List<RBlog> result = new List<RBlog>();   //返回结果集
            IList<int> originBlog =BlogLogic .GetOriginBlog(type, placeid);//获得原始微博列表
            foreach (int i in originBlog)
            {
                RBlog  tmp = GetSingleBlog("original", i);
                result.Add(tmp);
            }
            IList<int> tranBlog = BlogLogic.GetTranBlog(type, placeid);//获得转发微博列表
            foreach (int i in tranBlog)
            {
                RBlog tmp = GetSingleBlog("transmit", i);
                result.Add(tmp);
            }
            string str = JsonHelper.JsonSerializer<List<RBlog>>(result);//序列化
            return str;
        }
        public static string AddTranBlog(string placeId, string type, string originBlogId, string userId, string tranReason, string replyType)//转发微博
        {
            //placeId：转发微博地点编号
            //placeType：转发微博地点类型。取值：building、street
            //originBlogId：待转发的原始微博编号
            //userId：要进行转发微博操作的用户编号
            //tranReason：用户转发该条微博的理由
            //replyType：转发微博的回复类型.取值：1（不可回复），2（仅好友可回复），3（任何人都不可回复）
            //返回转发微博是否成功.
            int placeid = Int32.Parse(placeId);
            int originblogid = Int32.Parse(originBlogId);
            int userid = Int32.Parse(userId);
            int replytype = Int32.Parse(replyType);
            int b = BlogLogic.AddTranBlog(placeid, type, originblogid, userid, tranReason, replytype);
           //返回值：正数是转发微博成功后转发微博的ID,-1表示发微博出现异常，-2表示@消息分析失败
            if (b < 0)
            {
                return null;//转发微博发生错误，未处理
            }
            RBlog  result = GetSingleBlog("transmit", b);
            string str = JsonHelper.JsonSerializer<RBlog>(result);
            return str;
        }
        public static void DeleteBlog(string type, string blogId)//删除微博
        {
            //type:transmit,original
            //blogId:要删除微博的编号
            int blogid = Int32.Parse(blogId);
            bool b = BlogLogic.DeleteBlog(type, blogid);
            //b==false表示删除失败，未处理
        }
        public static string GetBlogComment(string type, string blogId)//获得某条微博的评论列表
        {
            //type取值：original,transmit
            RCommentList cl = new RCommentList();
            int blogid = Int32.Parse(blogId);
            cl.blog = GetSingleBlog(type, blogid);        //加载博客内容
            cl.commenList = new List<RComment>();
            IList<int> comment = BlogLogic.GetBlogComment(type, blogid);//获取微博的评论列表
            foreach (int c in comment)
            {
                //将评论增加到对象中
                RComment tmp = GetSingleBlogComment(type, c);   //会不会知道去调用私有的静态函数呢？？？改名
                //一般tmp不会为null
                if (tmp != null)
                {
                    cl.commenList.Add(tmp);     
                }
            }
            string result = JsonHelper.JsonSerializer<RCommentList>(cl);//序列化对象
            return result;
        }
        public static string AddComment(string type, string blogId, string userId, string comment)//对微博发表评论
        {
            //type指明要被评论的微博的类型：取值只可能是:transmit,original
            //blogId指明微博的编号
            //userId指明要评论该微博的用户编号
            //comment指明用户的评论内容
            //返回值：刚发表的评论对象或者null
            int blogid = Int32.Parse(blogId);
            int userid = Int32.Parse(userId);
            int b = BlogLogic.AddComment(type, blogid, userid, comment);
            //返回值说明：正数是评论的ID,-1表示发表评论出现异常，-2表示@消息分析失败，-3参数type错误
            if (b < 0)
            {
                return null;    //评论失败，错误尚未处理
            }
            RComment result = GetSingleBlogComment(type, b);
            string str = JsonHelper.JsonSerializer<RComment>(result);
            return str;
        }
        public static void DeleteComment(string type, string commentId)//删除评论，权限判断接口？？？(可以判断删除)
        {
            //type:original,transmit
            int commentid = Int32.Parse(commentId);
            bool b = BlogLogic.DeleteComment(type, commentid);
            //b==false删除失败，尚未处理
        }
        public static string AddOriginalBlog(string placeId, string type, string userId, string content, string replyType)//发布微博
        {
         //replyType:取值1（不可回复）,2（仅好友可回复）,3（任何人都可回复）
            int placeid = Int32.Parse(placeId);
            int userid = Int32.Parse(userId);
            int replytype = Int32.Parse(replyType);
            int b = BlogLogic.AddOriginalBlog(placeid, type, userid, content, replytype);
            //返回值说明：正数是微博的ID,-1表示发微博出现异常，-2表示@消息分析失败
            if (b < 0)
            {
                return null;//转发微博发生错误，未处理
            }
            RBlog result = GetSingleBlog("original", b);
            string str = JsonHelper.JsonSerializer<RBlog>(result);
            return str;
        }

        #endregion
   
        #region 丁玉清
        #region 关注
        public static void AddAttention(string from, string to)//增加关注
        {
            int f = int.Parse(from);
            int t = int.Parse(to);
            bool b=BlogLogic.AddAttention(f, t);
            //b=false未进行处理
        }
        public static void DeleteAttention(string from, string to)//删除关注
        {
            int f = int.Parse(from);
            int t = int.Parse(to);
            bool b = BlogLogic.DeleteAttention(f, t);
            //b=false未处理
        }
        public static string GetAttentionList(string userId)//获取用户关注的好友列表
        {
            List<RUserInfo> result = new List<RUserInfo>();
            int userid = Int32.Parse(userId);
            List<int> user = BlogLogic.GetAttentionList(userid);
            foreach (int u in user)
            {
                RUserInfo temp =UserLogicControl.GetUserInfo(u);
                result.Add(temp);
            }
            return JsonHelper.JsonSerializer<List<RUserInfo>>(result);//序列化
    
        }
        public static string GetFansList(string userId)//获取用户的粉丝列表
        {
            List<RUserInfo> result = new List<RUserInfo>();
            int userid = Int32.Parse(userId);
            List<int> user = BlogLogic.GetFansList(userid);
            foreach (int u in user)
            {
                RUserInfo  temp =UserLogicControl.GetUserInfo(u);
                result.Add(temp);
            }
            return JsonHelper.JsonSerializer<List<RUserInfo >>(result);//序列化
        }
        #endregion
        #region 拉黑
        public static void AddBlack(string from, string to)//3.	增加拉黑
        {
            //用户from请求拉黑用户to
            int f = int.Parse(from);
            int t = int.Parse(to);
            bool b=BlogLogic.AddBlack(f, t);
            //b=false没有处理
        }
        public static void DeleteBlack(string from, string to)//4.	删除拉黑
        {
            int f = int.Parse(from);
            int t = int.Parse(to);
            bool b=BlogLogic.DeleteBlack(f, t);
            //b=false没有处理
        }
        #endregion
        #region @或评论列表
        public static string GetAllAtUserBlog(string userId)//获得在微博中或者评论中@用户的微博列表
        {
            int id = int.Parse(userId);
            List<RBlog> result = new List<RBlog>();   //返回结果集
            IList<int> originBlog = BlogLogic.GetAllAtOriginalBlog(id);//获得@用户原始微博列表
            foreach (int i in originBlog)
            {
                RBlog tmp = GetSingleBlog("original", i);
                result.Add(tmp);
            }
            IList<int> tranBlog = BlogLogic.GetAllAtTranBlog(id);//获得@用户的所有转发微博列表
            foreach (int i in tranBlog)
            {
                RBlog tmp = GetSingleBlog("transmit", i);
                result.Add(tmp);
            }
            string str = JsonHelper.JsonSerializer<List<RBlog>>(result);//序列化
            return str;
        }
        public static string GetAllCommentBlog(string userId)//获得用户的被评论的所有微博
        {
            int id = int.Parse(userId);
            List<RBlog> result = new List<RBlog>();   //返回结果集
            IList<int> originBlog = BlogLogic.GetAllCommentOriginalBlog(id);//获得用户被评论的原始微博列表
            foreach (int i in originBlog)
            {
                RBlog tmp = GetSingleBlog("original", i);
                result.Add(tmp);
            }
            IList<int> tranBlog = BlogLogic.GetAllCommentTranBlog(id);//获得用户被评论的所有转发微博列表
            foreach (int i in tranBlog)
            {
                RBlog tmp = GetSingleBlog("transmit", i);
                result.Add(tmp);
            }
            string str = JsonHelper.JsonSerializer<List<RBlog>>(result);//序列化
            return str;
        }
        #endregion
        #region 私信
        public string GetAllPrivateMsgList(string userId)//获得用户收到的私信列表
        {
            //userId:用户编号
            //返回值：序列化后的私信列表
            int id = int.Parse(userId);
            List<RPrivateMsg> result = new List<RPrivateMsg>(); //放回结果集
           List<int> list=BlogLogic.GetAllPrivateMsgList(id);   //得到用户收到的私信列表
           foreach (var i in list)
           {
               RPrivateMsg temp = GetSinglPrivateMsg(i);
               result.Add(temp);
           }
           string str = JsonHelper.JsonSerializer<List<RPrivateMsg >>(result);//序列化
           return str;
        }
        public static void AddPrivateMsg(string msgFrom, string msgTo, string msgContent)//发送私信
        {
            //msgFrom:发私信的用户
            //msgTo：收私信的用户
            //msgContent:私信内容
            //返回值：无
            int f = int.Parse(msgFrom);
            int t = int.Parse(msgTo);
            BlogLogic.AddPrivateMsg(f, t, msgContent);
        }
        #endregion
        #endregion
        #region 公用函数
        private static ROriginalMicroBlog GetOriginalBlog(int blogId)    //获得原始微博可序列化对象
        {
        
            var dbContext = new DigitwhuDataContext();
            OriginalMicroBlog temp = dbContext.GetTable<OriginalMicroBlog>().Single(c => c.originalBlogId==blogId);
            ROriginalMicroBlog  blog = new ROriginalMicroBlog();//可序列化对象
            blog.userId = temp.userId;
            blog.blogContent = temp.blogContent;
            blog.originalBlogId = temp.originalBlogId;
            blog.blogDateTime = temp.blogDateTime.ToString();
            blog.replyType = temp.replyType;
            blog.tranCount = temp.TranMicroBlog.Count;  //原始微博的转发次数
            blog.commentCount = temp.OriginBlogComment.Count;   //原始微博的评论次数
            return blog;
           
        }
        private static RTranMicroBlog  GetTranBlog(int blogId)//获得转发微博可序列化对象
        {
            var dbContext = new DigitwhuDataContext();
            TranMicroBlog   temp = dbContext.GetTable<TranMicroBlog>().Single(c => c.tranMicroBlogId== blogId);
            RTranMicroBlog blog = new RTranMicroBlog();//可序列化对象
            blog.userId = temp.userId;
            blog.tranReason  = temp.tranReason;
            blog.tranMicroBlogId  = temp.tranMicroBlogId ;
            blog.tranDateTime  = temp.tranDataTime .ToString();
            blog.replyType  = temp.replyType;
            blog.originalBlogId = temp.originalBlogId ;  //转发微博的原始微博
            blog.commentCount = temp.TranBlogComment.Count;   //转发微博的评论次数
            return blog;

        }
        private static RBlog  GetSingleBlog(string type, int blogId)//获取某条微博的对象
        { 
            //type取值：transmit,original
            var dbContext = new DigitwhuDataContext();
            if (type.Equals("transmit"))
            { 
                //转发微博
                RBlog  tmp = new RBlog ();
                tmp.tranBlog = GetTranBlog(blogId);
                tmp.tranUser = UserLogicControl.GetUserInfo(tmp.tranBlog.userId );
                tmp.originalBlog = GetOriginalBlog (tmp.tranBlog .originalBlogId);
                tmp.originalUser=UserLogicControl.GetUserInfo (tmp.originalBlog .userId);
                return tmp;
            }
            if (type.Equals("original"))
            { 
                //原创微博
                RBlog tmp = new RBlog();
                tmp.originalBlog = GetOriginalBlog(blogId);
                tmp.originalUser= UserLogicControl.GetUserInfo(tmp.originalBlog .userId);
                tmp.tranBlog = null;
                tmp.tranUser= null;
                return tmp;
            }
            return null;//参数错误
        }
        private static RComment GetSingleBlogComment(string type, int commentId)//获取某条评论的对象
        {
            //type取值：transmit,original
            var dbContext = new DigitwhuDataContext();
            if (type.Equals("transmit"))
            {
                //转发微博的评论
                RComment  tmp = new RComment();
                TranBlogComment temp= dbContext.TranBlogComment.Single(c => c.commentId == commentId);
                RCommentUnit unit = new RCommentUnit();
                unit.blogId = temp.tranMicroBlogId;
                unit.comment = temp.comment;
                unit.commentId = temp.commentId;
                unit.dateTime = temp.commentDateTime.ToString();
                unit.userId = temp.userId;
                tmp.comment= unit;
                tmp.userInfo =UserLogicControl.GetUserInfo(temp.userId);
                return tmp;
            }
            if (type.Equals("original"))
            {
                //原创微博的评论
                RComment tmp = new RComment();
                OriginBlogComment  temp = dbContext.OriginBlogComment.Single(c => c.commentId== commentId);
                RCommentUnit unit = new RCommentUnit();
                unit.blogId = temp.originalBlogId;
                unit.comment = temp.comment;
                unit.commentId = temp.commentId;
                unit.dateTime = temp.commentDateTime.ToString();
                unit.userId = temp.userId;
                tmp.comment = unit;
                tmp.userInfo =UserLogicControl.GetUserInfo(temp.userId);
                return tmp;
            }
            return null;//参数错误
        }
        private static RPrivateMsg GetSinglPrivateMsg(int msgId)//获取某条私信的可序列化对象
        {
            DigitwhuDataContext db = new DigitwhuDataContext();
            PrivateMsg temp = db.PrivateMsg.Single<PrivateMsg>(p => p.privateMsgId == msgId);
            RPrivateMsg result = new RPrivateMsg();
            if (temp == null)   //查询为空
            {
                return null;
            }
            result.msgContent = temp.msgContent;
            result.msgDateTime = temp.msgDateTime.ToString();
            result.privateMsgId = temp.privateMsgId;
            result.msgFromUser = UserLogicControl.GetUserInfo(temp.msgFrom);
            return result;
        }
       #endregion
    }
}